import "reflect-metadata";
import {createConnection, Not} from "typeorm";
import {User} from "./entity/User";
import {UserExtend} from "./entity/UserExtend";
import {Posts} from "./entity/Posts";
import {Tags} from "./entity/Tags";

createConnection().then(async connection => {
    // 1.查询全部的字段 select * from user;
    // const userRepository = connection.getRepository(User);
    // const result = await userRepository.find();
    // console.log(result);

    // 2 使用select选择性查询想要的字段 select id, username from user;
    // const userRepository = connection.getRepository(User);
    // const result = await userRepository.find({ select: ['id', 'username'] });
    // console.log(result);

    // 3.使用where条件查询 select * from user where user.id = 1;
    // const userRepository = connection.getRepository(User);
    // const result = await userRepository.find({ where: { id: 1 } });
    // console.log(result);

    // 使用where多条件and的关系查询数据 select * from user where user.id = 1 and user.username = 'xx';
    // const userRepository = connection.getRepository(User);
    // const result = await userRepository.find({ where: { id: 1, username: 'xx' } });
    // console.log(result);

    // 5.使用where..or查询数据 select * from user where user.id = 1 or user.username = 'xx';
    // const userRepository = connection.getRepository(User);
    // const result = await userRepository.find({ where: [{ id: 1 }, { username: 'xx' }] });
    // console.log(result);

    // 6.relations关系查询(前提是要先有外键关联关系) select a.*,b.* from user as a left join user_extend as b on a.id = b.userId;
    // const userRepository = connection.getRepository(User);
    // const result = await userRepository.find({ relations: ['userDetail','posts'] });
    // console.log(result);

    // 7.使用join
    // const userRepository = connection.getRepository(User);
    // const result = await userRepository.find({
    //     join: {
    //         alias: 'user',
    //         leftJoinAndSelect: {
    //             detail: 'user.userDetail',
    //             posts: 'user.posts'
    //         }
    //     }
    // });
    // console.log(result);

    // 8、order排序查询 select * from user order by username asc, id desc;
    // const userRepository = connection.getRepository(User);
    // const result = await userRepository.find({
    //     order: {
    //         id: 'DESC',
    //         username: 'ASC'
    //     }
    // });
    // console.log(result);

    // 9.分页查询 select * from user limit 0,10;
    // const userRepository = connection.getRepository(User);
    // const result = await userRepository.find({
    //     skip: 0,
    //     take: 10,
    // })
    // console.log(result);

    // 10.Not select * from user where username != '王五';
    // const userRepository = connection.getRepository(User);
    // const result = await userRepository.find({
    //     username: Not('王五')
    // });
    // console.log(result);
}).catch(error => console.log(error));
